package com.fire.job.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.Configuration;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import com.fire.job.constants.ConfConstants;







public class FileUtil {
	
	public static final String CLASSPATH_PREFIX = "classpath:"; 
	
	 public static final String HADOOP_CLASSPATH_PREFIX = "hadoop_classpath:";
    
    

	/**
	 * 从数组中找到一个文件输入流.数组中找到一个就退出
	 * @param location
	 * @return
	 * @throws IOException
	 */
	public static InputStream getStream(String[] location) throws IOException {
		InputStream is = null;
		for (int i = 0; i < location.length; i++) {
			is=getStream(location[i]);
			if(is!=null)
				break;
		}
		return is;
	}
	/** 通过路径获得输入流，路径不区分绝对还是相对（都会去找） */
	public static InputStream getStream(String location) throws IOException {
		InputStream is = null;
		if (location.startsWith(CLASSPATH_PREFIX)) {
			is = openClasspathStream(location);
			if (is == null) {
				is = openClasspathStream(switchPath(location));
			}
		}else {
			File file = new File(location);
			if (file.exists()) {
				byte[] bs = FileUtils.readFileToByteArray(file);
				is = new ByteArrayInputStream(bs);
			} 
		}
		return is;
	}
	
	/** 通过目录路径获得目录下所有文件的输入流，路径不区分绝对还是相对（都会去找） */
	public static List<Pair<String,InputStream>> getdirectoryStream(String location,IOFileFilter... filters) throws IOException {
		List<Pair<String,InputStream>> isList = new ArrayList<Pair<String,InputStream>>();
		String  filePath = null;
		if (location.startsWith(CLASSPATH_PREFIX)) {
			filePath = getFilePath(location);
			if (filePath == null) {
				filePath = getFilePath(switchPath(location));
			}
		}else {
			File file = new File(location);
			if (file.exists()) {
				filePath = file.getPath();
			} 
		}
		if(filePath!=null){
			File directory = new File(filePath);
			if(directory.exists()){
				List<IOFileFilter> filterList = toList(filters);
				filterList.add(FileFilterUtils.fileFileFilter());
				File[] files =  directory.listFiles((FilenameFilter)new AndFileFilter(filterList));
				for (File file : files) {
					byte[] bs = FileUtils.readFileToByteArray(file);
					InputStream is = new ByteArrayInputStream(bs);
					if(is!=null)
						isList.add(new Pair<String,InputStream>(file.getName(),is));
				}
				
			}
		}
		
		
		return isList;
	}
	/**
	 * 
	 * @param filters
	 * @return
	 */
	 public static List<IOFileFilter> toList(IOFileFilter... filters) {
	        List<IOFileFilter> list = new ArrayList<IOFileFilter>();
	        if(filters!=null){
	        	for (int i = 0; i < filters.length; i++) {
		            if (filters[i] == null) {
		                throw new IllegalArgumentException("The filter[" + i + "] is null");
		            }
		            list.add(filters[i]);
	        	}
	        }
	        return list;
	    }
	 
	private static InputStream openClasspathStream(String location) throws IOException {
		InputStream is = null;
		String path = location.substring(ConfConstants.CLASSPATH_URL_PREFIX.length());
		URL url = FileUtils.class.getResource(path);
		if (url != null) {
			is = url.openStream();
		}
		return is;
	}
	private static String getFilePath(String location) throws IOException {
		String  filePath = null;
		String path = location.substring(ConfConstants.CLASSPATH_URL_PREFIX.length());
		URL url = FileUtils.class.getResource(path);
		if (url != null) {
			filePath =url.getFile();
		}
		return filePath;
	}
	

	/** 绝对路径、相对路径的相互转换 */
	public static String switchPath(String path) {
		if (path.startsWith("/")) {
			return path.substring(1);
		} else if (path.startsWith(CLASSPATH_PREFIX)) {
			String _path = path.substring(CLASSPATH_PREFIX.length());
			if (_path.startsWith("/")) {
				return CLASSPATH_PREFIX + _path.substring(0);
			} else {
				return CLASSPATH_PREFIX + "/" + _path;
			}
		}
		else if (path.startsWith(HADOOP_CLASSPATH_PREFIX)) {
			String _path = path.substring(HADOOP_CLASSPATH_PREFIX.length());
			if (_path.startsWith("/")) {
				return HADOOP_CLASSPATH_PREFIX + _path.substring(0);
			} else {
				return HADOOP_CLASSPATH_PREFIX + "/" + _path;
			}
		}
		return "/" + path;
	}
	
	
}
